﻿# MUST HAVE

# Kinetic Extension
Kinetic.Node.prototype.getBound = ->
	pos = @getPosition()		
	x:pos.x
	y:pos.y
	w:@attrs.width
	h:@attrs.height
	r:=>
		pos.x+@width
	b:=>
		pos.y+@height
Kinetic.Node.prototype.setBound = (bound)->
	if bound is undefined
		return
	if bound.x isnt undefined
		@setX bound.x
	if bound.y isnt undefined
		@setY bound.y

class UserControl
	constructor:(@attrs) ->  
		@layer = attrs.layer		
		@width = attrs.width
		@height = attrs.height
		@.text = attrs.text		
		@group = new Kinetic.Group 
			draggable: attrs.draggable
			dragConstraint: attrs.dragConstraint			
			x:@attrs.x
			y:@attrs.y		
		
		@selected = false

		@Me = @group
		@group.UserControl = @

	setSelected:(@selected) ->  
	dispose: ->
		@layer.remove(@group)
	typeName: 'UserControl'
	getBound:->
		pos = @group.getPosition()		
		x:pos.x
		y:pos.y
		w:@width
		h:@height
		r:=>
			pos.x+@width
		b:=>
			pos.y+@height
	setBound:(bound)->
		if bound is undefined
			return
		if bound.x isnt undefined
			@group.setX bound.x
		if bound.y isnt undefined
			@group.setY bound.y
	addChild: (child) ->
		if child.Me isnt undefined
			@group.add child.Me
		else
			@group.add child


###
IUserControl

getBound:->{x,y,w,h}
setBound:({x,y,w,h})->
addChild:(child)->group.Add

###	

class Panel extends UserControl
	addChild:(child)->
		@group.add child
		@arrangeChildren()
	arrangeChildren:->


class DockPanel extends Panel	
	constructor:(@attrs)->
		super @attrs					
		
	arrangeChildren: ->
		bound = @getBound()
		left = 0
		right = bound.w
		top = 0
		bottom = bound.h

		fillChildren = []
		for child in @group.children
			if child.dock is undefined or child.dock is 'none'
				continue
			if child.dock is 'fill'
				fillChildren.push child
				continue
			if child.dock is 'left'
				child.attrs.x = left
				child.attrs.height = bottom-top
				left+=child.attrs.width
				continue
			if child.dock is 'right'
				child.attrs.x = right - child.attrs.width
				right-=child.attrs.width
				continue

			# top/bottom will be implemented later

		for child in fillChildren
			child.attrs.x = left
			child.attrs.width = right - left
	setWidth:(@width) =>
		super @width
		@arrangeChildren()
	setX:(x) =>
		@group.setX x
class AutoVerticalOrderPanel extends Panel
	addChild:(child)->
		child.setDraggable true
		child.attrs.dragConstraint = "horizontal"
		child.on "dragmove", =>
			@arrangeChildren()
		super child
	arrangeChildren:->
		bound = @getBound()
		left = 0
		right = bound.w
		top = 0
		bottom = bound.h
		rowList = []
		rowIndex =0
		rowList.push
			rIndex: rowIndex
			items:[]

		for child in @group.children			
			cb = child.getBound()
			x = cb.x
			w = cb.w
			
			# new row
			if x<left
				rowIndex++
				rowList.push
					rIndex:rowIndex
					items:[]	
				left = 0			

			rowList[rowIndex].items.push child
			left = x+w

		for row in rowList
			maxH=0
			for item in row.items
				item.setBound
					y:top
				cb = item.getBound()
				if cb.h>maxH
					maxH = cb.h
			top=top+maxH